#' 根据res创建Table 1
#'
#' @param res U5_mr分析出的res
#'
#' @return Table 1
#' @export
#'
#' @examples
#'
#' \dontrun{
#' Table1 <- U6_create_table1(res)
#' }
#'
U6_create_table1<-function( res ){

  ao <- U1_get_ao()

  if( !"ncase.exposure" %in% colnames(res)){ res$ncase.exposure<-NA }
  if( !"ncontrol.exposure" %in% colnames(res)){ res$ncontrol.exposure<-NA }

  exposure <- unique( res[,c("id.exposure","exposure","ncase.exposure","ncontrol.exposure","samplesize.exposure")] )


  for (i in 1:nrow( exposure)) {
    if(exposure$id.exposure[i] %in% ao$id){
      exposure$population[i] <-  ao$population[which(ao$id==exposure$id.exposure[i])]
      exposure$pmid[i] <-  ao$pmid[which(ao$id==exposure$id.exposure[i])]
    }else{
      exposure$population[i]<-NA
      exposure$pmid[i] <-NA
      exposure$id.exposure[i]<-NA
    }
  }
  exposure<-data.frame( Type="exposure",exposure )
  colnames(exposure)<-c( "Type","OPENGWAS_ID","Trait","Case","Control","Samplesize","Population","PMID" )



  if( !"ncase.outcome" %in% colnames(res)){ res$ncase.outcome<-NA }
  if( !"ncontrol.outcome" %in% colnames(res)){ res$ncontrol.outcome<-NA }


  outcome<-unique( res[,c("id.outcome","outcome","ncase.outcome","ncontrol.outcome","samplesize.outcome")] )


  for (i in 1:nrow( outcome)) {
    if(outcome$id.outcome[i] %in% ao$id){
      outcome$population[i] <-  ao$population[which(ao$id==outcome$id.outcome[i])]
      outcome$pmid[i] <-  ao$pmid[which(ao$id==outcome$id.outcome[i])]
    }else{
      outcome$population[i]<-NA
      outcome$pmid[i] <-NA
      outcome$id.outcome[i]<-NA
    }
  }
  outcome<-data.frame( Type="outcome",outcome )
  colnames(outcome)<-c( "Type","OPENGWAS_ID","Trait","Case","Control","Samplesize","Population","PMID" )

  Table1<- plyr::rbind.fill(exposure,outcome)

  return(Table1)
}
